/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_XMLCreateNode.
	/// </summary>
	public class Block_XMLCreateNode : Block
	{

		/// <summary>
		/// Definitions for each node type (.seq format)
		/// </summary>
		enum SEQ_NODE_TYPE
		{
			Element = 1,
			Attribute = 2,
			Text = 3,
			CDATA_Section = 4,
			Entity_Reference = 5,
			Processing_Instruction = 7,
			Comment = 8,
			Document_Fragment = 11
		}

		enum STUDIO_NODE_TYPE
		{
			Element = 0,
			Attribute,
			Text,
			CDATA_Section,
			Entity_Reference,
			Processing_Instruction,
			Comment,
			Document_Fragment
		}

		private string _docHandle;
		private string _param1;
		private string _param2;
		private string _nodeHandle;

		private SEQ_NODE_TYPE _nodeType;


		private STUDIO_NODE_TYPE GetStudioNodeType(SEQ_NODE_TYPE seqType)
		{
			switch (seqType)
			{
				case SEQ_NODE_TYPE.Element:
					return STUDIO_NODE_TYPE.Element;

				case SEQ_NODE_TYPE.Attribute:
					return STUDIO_NODE_TYPE.Attribute;

				case SEQ_NODE_TYPE.Text:
					return STUDIO_NODE_TYPE.Text;

				case SEQ_NODE_TYPE.CDATA_Section:
					return STUDIO_NODE_TYPE.CDATA_Section;

				case SEQ_NODE_TYPE.Entity_Reference:
					return STUDIO_NODE_TYPE.Entity_Reference;

				case SEQ_NODE_TYPE.Processing_Instruction:
					return STUDIO_NODE_TYPE.Processing_Instruction;

				case SEQ_NODE_TYPE.Comment:
					return STUDIO_NODE_TYPE.Comment;

				case SEQ_NODE_TYPE.Document_Fragment:
					return STUDIO_NODE_TYPE.Document_Fragment;

				default:
					Trace.Info("Block_XMLCreateNode::GetStudioNodeType - no type for " + seqType);
					Settings.Instance.Unimplemented("Block_XMLCreateNode");
					return STUDIO_NODE_TYPE.Document_Fragment;
			}
		}


		public override TYPE GetBlockType()
		{
			return TYPE.XMLCreateNode;
		}

		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_XMLCreateNode::GetXML Started.");	
			// <XMLCreateNode name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<XMLCreateNode name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			
			// ...documentHandle="HANDLE" >
			xml += " documentHandle=\"" + Utils.XmlString(_docHandle) + "\"";

			// ...nodeType="NODE_TYPE" >
			int id = (int)GetStudioNodeType(_nodeType);
			xml += " nodeType=\"" + id + "\"";

			// ...param1="PARAM_1" >
			xml += " param1=\"" + Utils.XmlString(_param1) + "\"";

			// ...param2="PARAM_2" >
			xml += " param2=\"" + Utils.XmlString(_param2) + "\"";

			// ...nodeHandle="NODE_HANDLE" >
			xml += " nodeHandle=\"" + Utils.XmlString(_nodeHandle) + "\">\r\n";

			// <Inputs>
			xml += GetInputsXML(_blockId);
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			// </XMLCreateNode>
			xml +="\t\t\t</XMLCreateNode>\r\n";
			return xml;
		}


		public Block_XMLCreateNode(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.

			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));
			outputErrorIds.Add(new BlockOutput(OUTPUT.Exist, 135));
			outputErrorIds.Add(new BlockOutput(OUTPUT.InvalidHandle, 162));
			outputErrorIds.Add(new BlockOutput(OUTPUT.Failed_2, 141));
			
			Trace.Debug("Block_XMLCreateNode created.");

			_nodeType = (SEQ_NODE_TYPE)_rawData[_currentPos + 16];

			_currentPos += 20;

			// Document handle
			_docHandle = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// First parameter
			_param1 = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// second parameter
			_param2 = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// node handle
			_nodeHandle = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			
			Trace.Debug("Node type: " + _nodeType + ", Doc handle: " + _docHandle +
				", Node handle: " + _nodeHandle	+ ", param1: " + _param1 + 
				", param2: " + _param2);
		}
	}
}
